package makeChanges.controller;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sun.xml.internal.ws.wsdl.writer.document.ParamType;

import model.DatabaseException;
import model.User;
import model.Utilities;

/**
 * Servlet implementation class FinalizePrices
 */
@WebServlet("/FinalizePrices")
public class FinalizePrices extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		if(user==null || !user.getAccountType().equals("S")){
			response.sendRedirect("Login.html");
			return;
		}	
		
		Map<String,String[]> parameterMap = request.getParameterMap();
		String[] ids = parameterMap.get("id[]");
		String[] prices = parameterMap.get("price[]");
		String[] promotionalPrices = parameterMap.get("promotionalPrice[]");
		try {
			Utilities.updatePrices(ids, prices, promotionalPrices);
			response.sendRedirect("/Store/Supplier.html");
		} catch (DatabaseException e) {
			e.printStackTrace();
			response.sendRedirect("/Store/TechnicalDifficulties.html");
		}
	}

}
